import Vue from "vue";
import Router from "vue-router";
import Layout from "../layout";
import History from "../util/history";
Vue.use(Router);
Vue.use(History);
// 实例化之前，扩展Router
Router.prototype.goBack = function () {
  this.isBack = true;
  this.back();
};

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
const originalReplace = Router.prototype.replace;
Router.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};
const router = new Router({
  routes: [{
    path: "/",
    component: Layout,
    redirect: "/index",
    children: [{
        path: "/login",
        component: () => import("@/views/login"),
        name: "Login",
        meta: {
          title: "登录"
        }
      },
      {
        path: "/index",
        component: () => import("@/views/index/index"),
        name: "Dashboard",
        meta: {
          title: "首页"
        }
      },
      {
        path: "/train",
        component: () => import("@/views/index/train"),
        name: "Train",
        redirect: "/train/index",
        meta: {
          title: "安全培训"
        },
        children: [{
            path: "/train/index",
            component: () => import("@/views/train/default"),
            name: "TrainDef",
            meta: {
              title: "安全培训"
            }
          },
          {
            path: "/train/search",
            component: () => import("@/views/train/search"),
            name: "TrainSearch",
            meta: {
              title: "安全培训"
            }
          },
          {
            path: "/train/detail/:id",
            component: () => import("@/views/train/detail"),
            name: "TrainDetail",
            meta: {
              title: "安全培训"
            }
          },
          {
            path: "/train/play/:id",
            component: () => import("@/views/train/play"),
            name: "TrainPlay",
            meta: {
              title: "安全培训"
            }
          },
          {
            path: "/train/assess/:id",
            component: () => import("@/views/assess/index"),
            name: "TrainAssess",
            meta: {
              title: "安全培训考核"
            }
          },
          {
            path: "/train/assess/exam/:id",
            component: () => import("@/views/assess/exam"),
            name: "TrainAssessExam",
            meta: {
              title: "安全培训考核"
            }
          }
        ]
      },
      {
        path: "/manageTrainee",
        component: () => import("@/views/index/manageTrainee"),
        name: "ManageTrainee",
        // redirect: "/manageTrainee/student/attendance",
        meta: {
          title: "培训分析"
        }
        // children: [
        //   {
        //     path: "/manageTrainee/student/attendance",
        //     component: () => import("@/views/student/attendance"),
        //     name: "ManageTraineeAttendance",
        //     meta: {
        //       title: "学员签到"
        //     }
        //   },
        //   {
        //     path: "/manageTrainee/student/attendance/:id",
        //     component: () => import("@/views/student/student"),
        //     name: "ManageTraineeAttendanceS",
        //     meta: {
        //       title: "学员签到"
        //     }
        //   },
        //   {
        //     path: "/manageTrainee/student/score",
        //     component: () => import("@/views/student/score"),
        //     name: "ManageTraineescore",
        //     meta: {
        //       title: "成绩排行"
        //     }
        //   },
        //   {
        //     path: "/manageTrainee/student/score/detail",
        //     component: () => import("@/views/student/scoreDetail"),
        //     name: "ManageTraineescoreDetail",
        //     meta: {
        //       title: "成绩详情"
        //     },
        //     children: [
        //       {
        //         path: "/manageTrainee/student/score/detail/:id",
        //         component: () => import("@/views/student/score/index"),
        //         name: "ManageTraineescoreDetailIndex",
        //         meta: {
        //           title: "成绩详情"
        //         }
        //       },
        //       {
        //         path: "/manageTrainee/student/score/detail/examination/:id",
        //         component: () => import("@/views/student/score/detail"),
        //         name: "ManageTraineescoreDetailIndex",
        //         meta: {
        //           title: "成绩详情"
        //         }
        //       }
        //     ]
        //   },
        //   {
        //     path: "/manageTrainee/student/oneScore",
        //     component: () => import("@/views/student/oneScore"),
        //     name: "ManageTraineeoneScore",
        //     meta: {
        //       title: "成绩查询"
        //     }
        //   }
        // ]
      },
      {
        path: "/manageTrainee/statistics",
        component: () => import("@/views/student/statistics"),
        name: "statistics",
        meta: {
          title: "数据统计"
        }
      }
    ]
  }]
});
// afterEach记录历史记录
router.afterEach((to, from) => {
  if (router.isBack) {
    // 后退
    History.pop();
    router.isBack = false;
    router.transitionName = "route-back";
  } else {
    History.push(to.path);
    router.transitionName = "route-forward";
  }
});
export default router;
